namespace Web.Control.WWW.Wiki
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using Lx.Web.Common.UI;
    using Lx.Web.Common.ControlsUI;
    using System.Collections;
    using System.Collections.Generic;
    using System.Xml;
    using System.IO;
    using System.Text;

    using BLF;
    using BLF.WWW_WWW.Wiki;
    using Model.WWW_WWW.Wiki;

    public class Diff : BaseControl
    {
        protected Literal lblDiff;
        
      
        protected override void OnLoad(EventArgs e)
        {
            PrintDiff();
            base.OnLoad(e);
        }

        public void PrintDiff()
        {
            if (Request["Page"] == null || Request["Rev1"] == null || Request["Rev2"] == null)
            {
                Redirect();
                return;
            }

            StringBuilder sb = new StringBuilder();

            PageInfo page = Pages.Instance.FindPage(Request["Page"]);
            if (page == null)
            {
                Redirect();
                return;
            }

            int rev1 = -1, rev2 = -1;
            try
            {
                rev1 = int.Parse(Request["Rev1"]);
                rev2 = int.Parse(Request["Rev2"]);
            }
            catch { }
            if (rev1 == -1 || (rev2 == -1 && !Request["Rev2"].ToLower().Equals("current")))
            {
                Redirect();
                return;
            }

            PageContent content = BLF.WWW_WWW.Wiki.Content.GetPageContent(page, true);

            sb.Append(@"<div id=""title"" class=""h1"">");
            sb.Append(Exchanger.ResourceExchanger.GetResource("DiffingPage").Replace("##PAGETITLE##", content.Title).Replace("##REV1##", rev1.ToString()).Replace("##REV2##", (rev2 != -1 ? rev2.ToString() : Exchanger.ResourceExchanger.GetResource("Current"))));
            sb.Append("</div>");

            sb.Append(@"<table style=""padding: 0px;"" class=""box""><tr><td><p><b><a href=""/knowledge/history.htm?Page=");
            sb.Append(Tools.UrlEncode(Request["Page"]));
            sb.Append("&amp;Rev1=");
            sb.Append(Request["Rev1"]);
            sb.Append("&amp;Rev2=");
            sb.Append(Request["Rev2"]);
            sb.Append(@""">&laquo; ");
            sb.Append(Exchanger.ResourceExchanger.GetResource("BackToHistory"));
            sb.Append("</a></b> - ");
            sb.Append(Exchanger.ResourceExchanger.GetResource("AddedRemovedLines"));
            sb.Append("</p></td></tr></table><br />");

            PageContent rev1Content = Pages.Instance.GetBackupContent(page, rev1);
            PageContent rev2Content = null;
            if (rev2 != -1)
            {
                rev2Content = Pages.Instance.GetBackupContent(page, rev2);
            }
            else
            {
                rev2Content = content;
            }

            string result = DiffTools.DiffRevisions(rev1Content.Content, rev2Content.Content);

            sb.Append(result);

            lblDiff.Text = sb.ToString();
        }

        private void Redirect()
        {
            Response.Redirect("/knowledge/default.htm");
        }
        
    }
}
